//
//  DappToolView.m
//  Bees
//
//  Created by NowYouCanSee on 2019/4/10.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "DappToolView.h"

@implementation DappToolView

+ (instancetype)showIsCollect:(BOOL)isCol complet:(ToolBlock)complet{
    DappToolView *v = [[DappToolView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, AdaptedWidth(120)) isCol:isCol];
    v.complet = complet;
    [LEEAlert actionsheet].config
    .LeeAddCustomView(^(LEECustomView *custom) {
        custom.view = v;
        custom.isAutoWidth = v;
    })
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeActionSheetBottomMargin(0.0f)
    .LeeCornerRadius(10.0f)
    .LeeConfigMaxWidth(^CGFloat(LEEScreenOrientationType type) {
        return CGRectGetWidth([[UIScreen mainScreen] bounds]);
    })
    .LeeShow();
    return v;
}
- (instancetype)initWithFrame:(CGRect)frame isCol:(BOOL)isCol{
    if(self == [super initWithFrame:frame]){
        self.isCol = isCol;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    NSMutableArray *itemArray = [NSMutableArray array];
    NSMutableArray *buttonArray = [NSMutableArray array];
    CGFloat buttonWidth = AdaptedWidth(60);
    CGFloat buttonMargin = (kScreenWidth-4*buttonWidth)/5.0;
    CGFloat buttonHeight = AdaptedWidth(80);

    [itemArray addObject:@{@"title" : XLocalized(@"刷新") , @"image" : @"tool_refresh" , @"seletImage" : @"tool_refresh" , @"type" : @(ToolTypeRefresh)}];
    [itemArray addObject:@{@"title" : XLocalized(@"复制链接") , @"image" : @"tool_link" , @"seletImage" : @"tool_refresh" , @"type" : @(ToolTypeCopy)}];
//    [itemArray addObject:@{@"title" : XLocalized(@"分享") , @"image" : @"tool_share" , @"seletImage" : @"tool_refresh" , @"type" : @(ToolTypeShare)}];
    [itemArray addObject:@{@"title" : XLocalized(@"收藏") , @"image" : @"tool_uncol" , @"seletImage" : @"tool_col" , @"type" : @(ToolTypeCollect)}];
    for (int i=0; i<itemArray.count; i++) {
        NSDictionary *info = [itemArray objectOrNilAtIndex:i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 100+i;
        [button setTitle:info[@"title"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:info[@"image"]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:info[@"seletImage"]] forState:UIControlStateSelected];
        [button.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
        [button setTitleColor:UIColorHex(#333333) forState:UIControlStateNormal];
        kWeakSelf(self);
        [button addTapBlock:^(UIButton * _Nonnull btn) {
            NSDictionary *info = [itemArray objectOrNilAtIndex:btn.tag-100];
            if (info) {
                [LEEAlert closeWithCompletionBlock:^{
                    !weakself.complet?:weakself.complet([info[@"type"] integerValue]);
                }];
            }
        }];
        [buttonArray addObject:button];
        [self addSubview:button];
        if (i == itemArray.count-1) {
            button.selected = self.isCol;
        }
        if (i == 0) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(buttonMargin);
                make.centerY.mas_equalTo(0);
                make.width.mas_equalTo(buttonWidth);
                make.height.mas_equalTo(buttonHeight);
            }];
        }else{
            UIButton*btn = buttonArray[[buttonArray indexOfObject:button] - 1];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(btn.mas_right).offset(buttonMargin);
                make.centerY.mas_equalTo(0);
                make.width.mas_equalTo(buttonWidth);
                make.height.mas_equalTo(buttonHeight);
            }];
        }
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:AdaptedWidth(13)];
    }
  
}
- (void)dealloc{
    
}
@end
